package com.denso.store.admin.templategroup;

import com.denso.store.common.model.TemplateGroup;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 * Created by lijian on 2017/10/24.
 */
public class TemplateGroupController extends Controller {

    static TemplateGroupService service = new TemplateGroupService();

    public void index() {
        List<TemplateGroup> templateGroupList = service.getAll();
        setAttr("templateGroupList", templateGroupList);
        render("index.html");
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        TemplateGroup templateGroup = getModel(TemplateGroup.class);
        service.save(templateGroup);
        setAttr("notice", "添加成功");
        index();
    }

    public void edit() {
        int id = getParaToInt("id");
        TemplateGroup templateGroup = service.findById(id);
        setAttr("templateGroup", templateGroup);
        render("edit.html");
    }

    public void update() {
        TemplateGroup templateGroup = getModel(TemplateGroup.class);
        service.update(templateGroup);
        setAttr("notice", "修改成功");
        index();
    }

    public void delete() {
        int id = getParaToInt("id");
        // 删除模板下关联的计划条目
        int count = Db.update("delete from template where groupId=?", id);
        service.deleteById(id);
        setAttr("notice", "删除成功");
        index();
    }
}
